package com.ah.proj.bus.address.dao;

import java.sql.SQLException;
import java.util.*;

import com.ah.proj.bean.Address;
import com.ah.proj.util.BaseDao;

// 增删改查
public class AddressDao extends BaseDao {
	public List<Address> getAll() {
		List<Address> lst = new ArrayList<Address>();
		super.getConn();
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(" SELECT ");
			sb.append("  id,");
			sb.append("  address");
			sb.append(" FROM ");
			sb.append("  address_info ");
			sb.append(" WHERE isDelete = '0' ");
			super.psta = super.conn.prepareStatement(sb.toString());
			super.rs = psta.executeQuery();
			while (rs.next()) {
				Address address = new Address();
				address.setId(rs.getInt("id"));
				address.setAddress(rs.getString("address"));
				lst.add(address);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			super.closeConn();
		}
		return lst;
	}

	public Address getById(int id) {
		Address address = null;
		super.getConn();
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(" SELECT ");
			sb.append("  id,");
			sb.append("  address,");
			sb.append("  tel");
			sb.append(" FROM ");
			sb.append("  address_info ");
			sb.append(" WHERE");
			sb.append("  id = ? AND isDelete = '0' ");
			super.psta = super.conn.prepareStatement(sb.toString());
			super.psta.setInt(1, id);
			super.rs = psta.executeQuery();
			if (rs.next()) {
				address = new Address();
				address.setId(rs.getInt("id"));
				address.setAddress(rs.getString("address"));
				address.setTel(rs.getString("tel"));
				address.setIsDelete("0");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			super.closeConn();
		}
		return address;
	}

	public boolean insert(Address addr) {
		super.getConn();
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(" INSERT INTO address_info");
			sb.append(" VALUES ");
			sb.append(" (null,?,?,'0') ");
			super.psta = super.conn.prepareStatement(sb.toString());
			super.psta.setString(1, addr.getAddress());
			super.psta.setString(2, addr.getTel());
			int result = psta.executeUpdate();
			if (result > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			super.closeConn();
		}
		return false;
	}

	public boolean delete(int id) {
		super.getConn();
		try {
			String sql = "DELETE FROM address_info WHERE id = ?";
			super.psta = super.conn.prepareStatement(sql);
			super.psta.setInt(1, id);
			int result = psta.executeUpdate();
			if (result > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			super.closeConn();
		}
		return false;
	}

	public boolean update(Address addr) {
		super.getConn();
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(" UPDATE address_info");
			sb.append(" SET ");
			sb.append("   address=?, ");
			sb.append("   tel=?, ");
			sb.append("   isDelete=? ");// 逻辑删除时用
			sb.append(" WHERE ");
			sb.append("   id = ?");
			super.psta = super.conn.prepareStatement(sb.toString());
			super.psta.setString(1, addr.getAddress());
			super.psta.setString(2, addr.getTel());
			super.psta.setString(3, addr.getIsDelete());
			super.psta.setDouble(4, addr.getId());
			int result = psta.executeUpdate();
			if (result > 0) {
				System.out.println("更新成功！");
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			super.closeConn();
		}
		return false;
	}

	// 测试
	public static void main(String[] args) {
		AddressDao dao = new AddressDao();
		System.out.println("---getAll");
		List<Address> all = dao.getAll();
		for (Address address : all) {
			System.out.println(address);
		}
		System.out.println("---getById");
		System.out.println(dao.getById(2));
		System.out.println("---insert");
		Address a = new Address();
		a.setAddress(new java.util.Date().toString());
		a.setTel("13045678901");
		dao.insert(a);
		System.out.println("---update");
		Address update = dao.getById(3);
		update.setAddress(update.getAddress() + "A");
		dao.update(update);
		System.out.println("---delete");
		dao.delete(4);
	}
}